package com.ssm.controller;

import com.ssm.pojo.Customer;
import com.ssm.pojo.Product;
import com.ssm.pojo.Quotation;
import com.ssm.service.CustomerService;
import com.ssm.service.ProductService;
import com.ssm.service.QuotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/quotation")
public class QuotationController {
    @Autowired
    private QuotationService quotationService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ProductService productService;


    @RequestMapping("/list")
    public String quotationList(HttpServletRequest request, HttpServletResponse response) {
        return "quotation/quotation";
    }

    @RequestMapping("/listdata")
    public @ResponseBody Map<String,Object> tableData(Integer page, Integer limit,String productNO){
        List<Quotation> quoList = quotationService.selectQuoPage(productNO,page,limit);
        Integer count = quotationService.selectQuoSize(productNO);

        // 返回表格数据
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count",count);
        resultMap.put("data",quoList);
        return resultMap;
    }

    // 转到添加页面
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd(Model model){
        List<Customer> customers =  customerService.seleteCustomersPage(null,null,null);
        model.addAttribute("customers",customers);

        List<Product> products = productService.selectProductsPage(null,null,null);
        model.addAttribute("products",products);
        return "quotation/quotation_new";
    }

    // 保存添加的数据
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Quotation quotation){
        quotation.setQuotationTime(new Date());
        quotationService.insertQuo(quotation);
        return "quotation/quotation";
    }

    // 转到更新界面
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String toUpdate(Model model,String quotationNO){
        Quotation quotation =  quotationService.selectByQuotationNO(quotationNO);
        model.addAttribute("quotation",quotation);

        List<Customer> customers =  customerService.seleteCustomersPage(null,null,null);
        model.addAttribute("customers",customers);

        List<Product> products = productService.selectProductsPage(null,null,null);
        model.addAttribute("products",products);
        return "quotation/quotation_update";
    }

    // 保存更新
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Quotation quotation){
        quotationService.updateQuo(quotation);
        return "quotation/quotation";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(String quoNO){
        quotationService.deleteQuo(quoNO);
        Map<String,String> result = new HashMap<String, String>();
        result.put("status","true");
        result.put("msg","删除成功");
        return result;
    }



    private String getCode() {
        StringBuffer flag = new StringBuffer();

        String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了
        Random rand = new Random();
        for (int j = 0; j < 6; j++) {
            flag.append(sources.charAt(rand.nextInt(9)) + "");
        }
        System.out.println(flag.toString());

        return flag.toString();
    }
}

